<?php
use \Workerman\Autoloader;

require_once __DIR__ . '/../../../../../Workerman/Autoloader.php';
require_once __DIR__.'/../../../Config/Const.php';
Autoloader::setRootPath(__DIR__.'/../../..');

$page_groupId = GROUP_SYS_ANNOUNCEMENT_MNG;
$page_pageId = PAGE_MSG_TO_RESTAURANT;

require "../framepages/moudle_base.php";

$message_content = $_GET['message'];
$mode = $_POST['mode'];
$result = 'normal';
if ($mode == 'send_msg') {
    $message_content = $_POST['message_content'];
    $restaurant_id_array = $_POST['restaurant_id_array'];
    if (!$message_content) {
        $result = 'err_noMsg';
    } elseif (!$restaurant_id_array) {
        $result = 'err_noResId';
    } else {
        $system_message_to_restaurant_record = new \Data\System_message_to_restaurant(array());
        $system_message_to_restaurant_record->setMESSAGE_CONTENT($message_content);
        $system_message_to_restaurant_record->setRESTAURANT_ID_ARRAY(implode(',',$restaurant_id_array));

        \Data\System_message_to_restaurant_DAO::insert($system_message_to_restaurant_record);
        $result = 'succ_sendMsg';
    }
}

?>
<div class="main-content" style="margin-left: 10px;">
    <div class="search-well">
        <form class="form-inline" style="margin-top: 0px;">
            商铺检索 &nbsp;<input class="input-xlarge form-control"
                              placeholder="店铺名称" id="4sStoreName" type="text"> &nbsp;
            <button class="btn btn-default" type="button" id="searchButton">
                <i class="fa fa-search"></i> 查找
            </button>
        </form>
    </div>
    <div>
        <form action="" method="post">
            <input type="hidden" name="mode" id="mode" value="send_msg">
            <div>
                <table width="100%">
                    <tr>
                        <th width="8%">选择</th>
                        <th width="12%">商铺id</th>
                        <th width="12%">商铺名称</th>
                        <th width="15%">商铺联系人</th>
                        <th>联系电话</th>
                    </tr>
                    <?php

                    $needle = $_GET['needle'];
                    if ($needle) {
                        $restaurant_list = \Data\Restaurant_DAO::getListByNeedle($needle);
                        foreach ($restaurant_list as $restaurant_record) {
                            $restaurant_id = $restaurant_record->getRESTAURANT_ID();
                            $restaurant_name = $restaurant_record->getRESTAURANT_NAME();
                            $contact_name = $restaurant_record->getCONTACT_NAME();
                            $contact_number = $restaurant_record->getCONTACT_NUMBER();
                            echo "
                                <tr>
                                    <td><input type='checkbox' name= 'restaurant_id_array[]' id='restaurant_id_array[]' value='$restaurant_id'></td>
                                    <td>$restaurant_id</td>
                                    <td>$restaurant_name</td>
                                    <td>$contact_name</td>
                                    <td>$contact_number</td>                     
                                </tr>
                            ";
                        }
                    }

                    ?>

                </table>
            </div>
            <hr width="100%" size=1 color=#00ffff  noshade>
            <div>
                <div>
                    <div>
                        <span style="font-size: 18px">消息内容</span>
                    </div>
                    <br>
                    <div>
                        <textarea cols="50" rows="10" id="message_content" name="message_content" ><?php echo $message_content ?></textarea>
                    </div>
                    <div>
                        <input type="submit" class="btn btn-danger" value="发送">
                    </div>
                </div>
            </div>
        </form>
    </div>

    <?php
    require "../framepages/footer.php";
    ?>
</div>
</div>
<script src="../lib/bootstrap/js/bootstrap.js"></script>
<script>
    function init() {
        var msg = "<?php echo $result ?>";
        if (msg == 'err_noMsg') {
            alert('消息内容不能为空');
        } else if (msg == 'err_noResId') {
            alert('请选择商铺');
        } else if (msg == 'succ_sendMsg') {
            alert('发送成功');
        }
    }


    $('#searchButton').click(function() {
            needle = $('#4sStoreName').val();
            message = $('#message_content').val();
            url = "?needle="+needle;
            if (message) {
                url = url+"&message="+message;
            }
            window.location.href = url;
        }
    );
</script>
</body>
</html>
